package com.ewerp.mud.core.plugin;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;

import java.util.Arrays;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.ewerp.mud.commands.ICommandEngine;
import com.ewerp.mud.core.session.ISessionManager;

public class TestEwerpPluginManager {
    private IPluginManager pluginManager = null;

    MockPlugin cmdEngine = new MockPlugin(Arrays.asList(new Class<?>[] { ICommandEngine.class }));
    MockPlugin sessionManager = new MockPlugin(Arrays.asList(new Class<?>[] { ISessionManager.class }));

    @Before
    public void setup() {
        pluginManager = new EwerpPluginManager();
        pluginManager.addPlugin(cmdEngine);
        pluginManager.addPlugin(null, sessionManager);
    }

    @After
    public void breakDown() {

    }

    @Test
    public void testPluginsNoNamespace() {
        assertSame(cmdEngine, pluginManager.getPlugin(ICommandEngine.class));
        assertSame(sessionManager, pluginManager.getPlugin(ISessionManager.class));
    }

    @Test
    public void testPluginsDefaultNamespace() {
        assertSame(cmdEngine, pluginManager.getPlugin(null, ICommandEngine.class));
        assertSame(sessionManager, pluginManager.getPlugin(ISessionManager.class));
    }

    @Test
    public void testPluginsWithNamespace() {
        MockPlugin tempPlugin = new MockPlugin(Arrays.asList(new Class<?>[] { ICommandEngine.class }));
        pluginManager.addPlugin("test", tempPlugin);

        assertSame(tempPlugin, pluginManager.getPlugin("test", ICommandEngine.class));
        assertNotSame(tempPlugin, pluginManager.getPlugin(ICommandEngine.class));
    }
    
    @Test(expected = IllegalArgumentException.class)
    public void testAddNullPluginWithNamespace() {
        pluginManager.addPlugin("test", null);
    }
    
    @Test(expected = IllegalArgumentException.class)
    public void testAddNullPluginNoNamespace() {
        pluginManager.addPlugin(null);
    }
    
    @Test(expected = IllegalArgumentException.class)
    public void testGetNullClazzWithNamespace() {
        pluginManager.getPlugin("test", null);
    }
    
    @Test(expected = IllegalArgumentException.class)
    public void testGetNullClazzNoNamespace() {
        pluginManager.getPlugin(null);
    }
    
    @Test
    public void testPluginManagerIsSet() {
        assertNotNull(cmdEngine.getPluginManager());
        assertNotNull(sessionManager.getPluginManager());
    }
}
